package com.qh.resourcehub.program.audit.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qh.resourcehub.program.audit.service.SourceCodeAuditExamineService;
import com.qh.resourcehub.program.dto.SourceCodeAuditDto;
import com.qh.resourcehub.program.entity.ApproveProcessDO;
import com.qh.resourcehub.program.entity.ApproveProcessRecordDO;
import com.qh.resourcehub.program.entity.SecurityAssessmentDO;
import com.qh.resourcehub.program.entity.SourceCodeAuditDO;
import com.qh.resourcehub.program.mapper.ApproveProcessMapper;
import com.qh.resourcehub.program.mapper.ApproveProcessRecordMapper;
import com.qh.resourcehub.program.mapper.SourceCodeAuditMapper;
import com.qh.resourcehub.program.vo.AccessVo;
import com.qh.resourcehub.program.vo.SourceCodeAuditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Y6
 * @date: 2025/4/18 15:17
 */
@Service
public class SourceCodeAuditExamineServiceImpl implements SourceCodeAuditExamineService {
    @Autowired
    private SourceCodeAuditMapper sourceCodeAuditMapper;


    @Autowired
    private ApproveProcessMapper approveProcessMapper;

    @Autowired
    private ApproveProcessRecordMapper approveProcessRecordMapper;
    @Override
    public List<SourceCodeAuditVo> querySourceCodeAuditExamineData(SourceCodeAuditDto virtualDto) {
        List<SourceCodeAuditVo> result=new ArrayList<>();
        virtualDto.setInfoType("8");
        if (!StringUtils.isEmpty(virtualDto.getUserCode())) {
            result = sourceCodeAuditMapper.querySourceCodeAuditExamineData(virtualDto);
            for (SourceCodeAuditVo virtualVo : result) {
                virtualVo.setExamineType("1");
            }
            List<SourceCodeAuditVo> virtualVos = sourceCodeAuditMapper.querySourceCodeAuditExamineHistoryData(virtualDto);
            for (SourceCodeAuditVo virtualVo : virtualVos) {
                virtualVo.setExamineType("2");
                result.add(virtualVo);
            }
        }
        return result;
    }

    @Override
    public List<SourceCodeAuditVo> querySourceCodeAuditExamineDataNum(SourceCodeAuditDto virtualDto) {
        List<SourceCodeAuditVo> result=new ArrayList<>();
        virtualDto.setInfoType("8");
        if (!StringUtils.isEmpty(virtualDto.getUserCode())) {
            result = sourceCodeAuditMapper.querySourceCodeAuditExamineData(virtualDto);
            for (SourceCodeAuditVo sourceCodeAuditVo : result) {
                sourceCodeAuditVo.setExamineType("1");
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ApplicationReject(SourceCodeAuditDto virtualDto) {
        // 1. 驳回流程：将流程状态更新为"010"表示驳回
        LambdaUpdateWrapper<ApproveProcessDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                .set(ApproveProcessDO::getProcessStatus, "010");
        int updateCount = approveProcessMapper.update(null, updateWrapper);
        // 2. 修改当前审核流程记录表
        LambdaUpdateWrapper<ApproveProcessRecordDO> update=new LambdaUpdateWrapper<>();
        update.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
                .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                .eq(ApproveProcessRecordDO::getProcessStatus,virtualDto.getProcessStatus())
                .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg())
                .set(ApproveProcessRecordDO::getProcessStatus, "010");
        int upRecode=approveProcessRecordMapper.update(null, update);
        // 3. 返回操作结果（全部成功返回1，否则返回0）
        return (updateCount > 0 || upRecode > 0) ? 1 : 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ApplicationPass(SourceCodeAuditDto virtualDto) {
        int result = 0;
        if ("02".equals(virtualDto.getProcessStatus())) {
            LambdaUpdateWrapper<ApproveProcessDO> approveProcessDOLambdaQueryWrapper = new LambdaUpdateWrapper<>();
            approveProcessDOLambdaQueryWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "05");
            int updateProcess = approveProcessMapper.update(null, approveProcessDOLambdaQueryWrapper);
            //把审批意见存储当前审批用户对应的数据但是状态不变
            LambdaUpdateWrapper<ApproveProcessRecordDO> approveProcessRecordDOLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            approveProcessRecordDOLambdaUpdateWrapper.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
                    .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                    .eq(ApproveProcessRecordDO::getProcessStatus, "02")
                    .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg());
            int updateOpinion = approveProcessRecordMapper.update(approveProcessRecordDOLambdaUpdateWrapper);
            //新增审批记录表的下一个审批用户记录信息
            ApproveProcessRecordDO approveProcessRecordDO = new ApproveProcessRecordDO();
            approveProcessRecordDO.setRecordId(virtualDto.getProcessId());
            approveProcessRecordDO.setExamineUserId(virtualDto.getExamineUserId());
            approveProcessRecordDO.setProcessStatus("05");
            int insertNext = approveProcessRecordMapper.insert(approveProcessRecordDO);
            result = (updateProcess > 0 || updateOpinion > 0 || insertNext > 0) ? 1 : 0;
        } else if ("05".equals(virtualDto.getProcessStatus())) {
            //待信息中心相关科室审批数据服务室
            //审批表状态修改为03
            LambdaUpdateWrapper<ApproveProcessDO> approveProcessDOLambdaQueryWrapper = new LambdaUpdateWrapper<>();
            approveProcessDOLambdaQueryWrapper.eq(ApproveProcessDO::getProcessId, virtualDto.getProcessId())
                    .set(ApproveProcessDO::getProcessStatus, "06");
            int updateProcess = approveProcessMapper.update(null, approveProcessDOLambdaQueryWrapper);
            //把审批意见存储当前审批用户对应的数据但是状态不变
            LambdaUpdateWrapper<ApproveProcessRecordDO> approveProcessRecordDOLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            approveProcessRecordDOLambdaUpdateWrapper.eq(ApproveProcessRecordDO::getRecordId, virtualDto.getProcessId())
//                    .eq(ApproveProcessRecordDO::getExamineUserId, virtualDto.getExamineUserId())
                    .eq(ApproveProcessRecordDO::getProcessStatus, "05")
                    .set(ApproveProcessRecordDO::getRecordMsg, virtualDto.getRecordMsg())
                    .set(ApproveProcessRecordDO::getProcessStatus, "06");
            int updateOpinion = approveProcessRecordMapper.update(approveProcessRecordDOLambdaUpdateWrapper);
            LambdaUpdateWrapper<SourceCodeAuditDO> sourceCodeAuditDOLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            sourceCodeAuditDOLambdaUpdateWrapper.eq(SourceCodeAuditDO::getInfoId, virtualDto.getInfoId())
                    .set(SourceCodeAuditDO::getProcessFileUrl, virtualDto.getProcessFileUrl());
            int update = sourceCodeAuditMapper.update(null, sourceCodeAuditDOLambdaUpdateWrapper);
            result = (updateProcess > 0 || updateOpinion > 0 ||update>0) ? 1 : 0;
        }
        return result;
    }
}